package com.example.springbootServiceNetwork.demos.web.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springbootServiceNetwork.demos.web.Config.Result;
import com.example.springbootServiceNetwork.demos.web.Config.ResultEnum;
import com.example.springbootServiceNetwork.demos.web.Dao.ListCateDao;
import com.example.springbootServiceNetwork.demos.web.Dao.ListItemDao;
import com.example.springbootServiceNetwork.demos.web.Entity.ListCate;
import com.example.springbootServiceNetwork.demos.web.Entity.ListItem;
import com.example.springbootServiceNetwork.demos.web.Service.ListCateItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ListCateItemServiceImpl implements ListCateItemService {
    @Resource
    private ListItemDao listItemDao;
    @Resource
    private ListCateDao listCateDao;

     /**
     * 查询站点分类
     */
     @Override
     public Result queryCate(){
         Result result = new Result();
         List<ListCate> list =listCateDao.selectList(null);
         result.setCode(ResultEnum.SUCCESS.getCode());
         result.setMsg("分类查询成功");
         result.setData(list);
         return result;
     }

    /**
     * 添加站点分类
     */
    @Override
     public Result addCate(ListCate listCate){
         Result result = new Result();
         if (listCate.getImage()==null){
             result.setCode(ResultEnum.BADREQUEST.getCode());
             return result;
         }
         if (listCate.getName()==null){
             result.setCode(ResultEnum.BADREQUEST.getCode());
             return result;
         }
         try {
          int res =listCateDao.insert(listCate);
             if (res==1){
                 result.setCode(ResultEnum.SUCCESS.getCode());
                 result.setMsg("新增成功");
             }else {
                 result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                 result.setMsg("新增失败");
             }
         }catch (Exception e){
             result.setCode(ResultEnum.UNKNOWNERROR.getCode());
             result.setMsg(e);
         }
         return result;
     }

    /**
     * 更新站点分类
     */
    @Override
    public Result updateCate(ListCate listCate){
        Result result = new Result();
        try {
          int res =listCateDao.updateById(listCate);
            if (res==1){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("更新成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("更新失败");
            }
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(e);
        }
        return result;
    }

    /**
     * 删除站点分类
     */
    @Override
    public Result deleteCate(ListCate listCate){
        Result result = new Result();
        try {
         int res =listCateDao.deleteById(listCate);
         if (res==1){
             result.setCode(ResultEnum.SUCCESS.getCode());
             result.setMsg("删除成功");
         }else {
             result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
             result.setMsg("删除失败");
         }
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(e);
        }
        return result;
    }

    /**
     * 查询所有站点数据
     */
     @Override
     public Result queryItem(){
         Result result = new Result();
         List<ListCate> list =listCateDao.selectList(null);
         for (ListCate item:list){
             LambdaQueryWrapper<ListItem> lqw =new LambdaQueryWrapper<>();
             lqw.eq(ListItem::getCateId,item.getId());
             List<ListItem> itemList = listItemDao.selectList(lqw);
             item.setList((ArrayList<ListItem>) itemList);
         }
         result.setCode(ResultEnum.SUCCESS.getCode());
         result.setMsg("站点数据查询成功");
         result.setData(list);
         return result;
     }

    /**
     * 添加站点详细数据
     */
    @Override
    public Result addItem(ListItem listItem){
        Result result = new Result();
        if (listItem.getImage()==null){
            result.setCode(ResultEnum.BADREQUEST.getCode());
            return result;
        }
        if (listItem.getTitle()==null){
            result.setCode(ResultEnum.BADREQUEST.getCode());
            return result;
        }
        if (listItem.getCateId()==null){
            result.setCode(ResultEnum.BADREQUEST.getCode());
            return result;
        }
        if (listItem.getUrl()==null){
            result.setCode(ResultEnum.BADREQUEST.getCode());
            return result;
        }
        try {
            int res =listItemDao.insert(listItem);
            if (res==1){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("新增成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("新增失败");
            }
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(e);
        }
        return result;
    }

    /**
     * 更新站点详细数据
     */
    @Override
    public Result updateItem(ListItem listItem){
        Result result = new Result();
        try {
            int res =listItemDao.updateById(listItem);
            if (res==1){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("更新成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("更新失败");
            }
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(e);
        }
        return result;
    }

    /**
     * 删除站点详细数据
     */
    @Override
    public Result deleteItem(ListItem listItem){
        Result result = new Result();
        try {
            int res =listItemDao.deleteById(listItem);
            if (res==1){
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("删除成功");
            }else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("删除失败");
            }
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(e);
        }
        return result;
    }
}
